//
//  BSUIView.m
//  SuoSiApp
//
//  Created by zhang on 16/5/8.
//  Copyright © 2016年 Ym. All rights reserved.
//

#import "BSUIView.h"

@implementation BSUIView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

    }
    return self;
    
}



- (void)keyBoardWillChangeFrame:(NSNotification *)notification
{
    CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
        self.keybordHeight = frame.size.height;
    
    
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    
    if (_IschangekeyBoard == YES) {
        CGFloat duration = 0.3;
        [UIView animateWithDuration:duration animations:^{
            
            self.transform = CGAffineTransformTranslate(self.transform, 0, _keyboardOffset);
            _IschangekeyBoard = NO;
            
        }];
    }
    
    
    
    
}


- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    
    
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    CGPoint pp = [self convertPoint:CGPointMake(0, textField.bottom) toView:currentWindow];
    
    
    _keyboardOffset =  pp.y + 253 - kScrenHeight;
    
    if (253 + pp.y  > kScrenHeight ) {
        NSLog(@"%f",self.keybordHeight);
        
        CGFloat duration = .3;
        
        [UIView animateWithDuration:duration animations:^{
            
            self.transform = CGAffineTransformTranslate(self.transform, 0, -_keyboardOffset);
            _IschangekeyBoard = YES;
            
        }];
    } ;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField.tag != 506) {
        
        [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        return [self validateNumber:string];
    }
    return YES;
    
}
- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField ) {
        
        if (textField.tag == 500) {
            if (textField.text.length > 11) {
                textField.text = [textField.text substringToIndex:11];
            }
        } else if((textField.tag == 501)) {
            if (textField.text.length > 8) {
                textField.text = [textField.text substringToIndex:8];
            }
        }
    }
}
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
@end
